package 每日签到;

/**
 * @author 陈艺博
 * @date 2021-03-02 23:52
 * @description https://leetcode-cn.com/problems/range-sum-query-2d-immutable/
 **/
public class _304_range_sum_query_2d_immutable_二维区域和检索_矩阵不可变 {
    int[][] sums;

     public _304_range_sum_query_2d_immutable_二维区域和检索_矩阵不可变(int[][] matrix) {
        if (matrix.length <= 0 || matrix[0].length <= 0) {
            return;
        }
        int row = matrix.length;
        int column = matrix[0].length;
        sums = new int[row][column + 1];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                sums[i][j + 1] = sums[i][j] + matrix[i][j];
            }
        }
        System.out.println(sums);
    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
        if (sums == null) {
            return 0;
        }
        int sum = 0;
        for (int i = row1; i <= row2; i++) {
            sum += sums[i][col2 + 1] - sums[i][col1];
        }
        return sum;
    }
}
